﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LightingReflectionApp
{
    public class Surface
    {
        public double Height { get; set; } // Высота над полом
        public double SquareSide { get; set; } // Размер элементарного квадрата
        public Square[,] Squares { get; set; } // Матрица квадратов
        public Room Room { get; set; }

        public Surface(double height, Room room)
        {
            Height = height;
            SquareSide = room.SquareSide;
            Room = new Room(room.Length, room.Width, room.Height, room.SquareSide, room.FloorReflectionCoefficient, room.CeilingReflectionCoefficient);
            InitializeSquares();
        }

        private void InitializeSquares()
        {
            int rows = (int)(Room.Width / SquareSide);
            int cols = (int)(Room.Length / SquareSide);
            Squares = new Square[rows, cols];

            for (int i = 0; i < rows; i++)
            {
                for (int j = 0; j < cols; j++)
                {
                    double x = j * SquareSide + SquareSide / 2;
                    double y = i * SquareSide + SquareSide / 2;
                    Squares[i, j] = new Square(x, y);
                }
            }
        }

        // Метод присвоения
        public void Assign(Surface other)
        {
            if (other == null) throw new ArgumentNullException(nameof(other));

            if (Height != other.Height || SquareSide != other.SquareSide)
            {
                throw new InvalidOperationException("Поверхности должны иметь одинаковую высоту и размер квадрата.");
            }

            for (int i = 0; i < Squares.GetLength(0); i++)
            {
                for (int j = 0; j < Squares.GetLength(1); j++)
                {
                    Squares[i, j].Illumination = other.Squares[i, j].Illumination;
                }
            }
        }

        // Метод сложения
        public Surface Add(Surface other)
        {
            if (other == null) throw new ArgumentNullException(nameof(other));

            if (Height != other.Height || SquareSide != other.SquareSide)
            {
                throw new InvalidOperationException("Поверхности должны иметь одинаковую высоту и размер квадрата.");
            }

            Surface result = new Surface(Height, Room);

            for (int i = 0; i < Squares.GetLength(0); i++)
            {
                for (int j = 0; j < Squares.GetLength(1); j++)
                {
                    result.Squares[i, j].Illumination = Squares[i, j].Illumination + other.Squares[i, j].Illumination;
                }
            }

            return result;
        }

        // Метод глубокого копирования
        public Surface DeepCopy()
        {
            Surface copy = new Surface(Height, Room);

            for (int i = 0; i < Squares.GetLength(0); i++)
            {
                for (int j = 0; j < Squares.GetLength(1); j++)
                {
                    copy.Squares[i, j].Illumination = Squares[i, j].Illumination;
                }
            }

            return copy;
        }
    }
}
